<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/all.conf.php');
require_once(__PHY_WEB.__DIR_ADM.'/admin.inc.php');
require_once(__PHY_LIB.'/class/uEnroll.class.php');
require_once(__PHY_LIB.'/class/uUser.class.php');

$rReq = validate($_REQUEST, 'msg:u|action:e[list,edit,update,delete]');
$sMsg = $rReq['msg'];
$sAction = $rReq['action'];

if ('update' == $sAction) {
	$oOne = new uEnroll($_POST['id']);
	$nOldStatus = $oOne->get('status');
	if (0 < $nOldStatus) { die('该记录已操作过，不能第二次操作。'); }
	$rP = validate($_POST, 'status:i');
	if (0 < $rP['status']) {
		$oOne->set('status', $rP['status']);
		$oOne->update();
		$oOne->upgradeToArtist();
		$sMsg .= '审批通过，升级成功。';
	}
	elseif ($rP['status'] < 0) {
		$oOne->set('status', $rP['status']);
		$oOne->update();
		$sMsg .= '审批为不通过。';
	}
	else { ; }
	$sAction = 'list';
	$_GET['status'] = $rP['status'];
}
elseif ('delete' == $sAction) {
	$oOne = new uEnroll($_POST['id']);
	if ($oOne->delete()) { $sMsg .= '删除成功。'; }
	else { $sMsg .= '操作失败。'; }
	$sAction = 'list';
}

if ('edit' == $sAction) {
	$oOne = new uEnroll($_REQUEST['id']);
	$oUser = new uUser($oOne->get('user_id'), 'nick');
	$oOne->set('user_nick', $oUser->get('nick'));
	$oTpl->assign('rOne', $oOne->getData());
}
elseif ('list' == $sAction) {
	$rP = validate($_GET, 'status:i|pg:n');
	$oTpl->assign('rP', $rP);
	$rPager = array(
		'Sze' => 25,
		'Req' => 'pg',
		'Num' => max(1, $rP['pg']),
	);
	$oList = new uEnrollList();
	$oList->cfgWhere("status='{$rP['status']}'");
	$rPager['Cnt'] = $oList->countAll();
	$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
	$oList->cfgOrder("time_added DESC");
	$oList->load($rPager['Sze']*($rPager['Num'] - 1), $rPager['Sze']);
	while ($oOne = $oList->nextItem()) {
		$oUser = new uUser($oOne->get('user_id'), 'nick');
		$oOne->set('user_nick', $oUser->get('nick'));
	}
	$oTpl->assign('rList', $oList->fetchArray());
	$oTpl->assign('rPager', $rPager);
}

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $sAction);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
